import { getCarList } from "@/api/car"

export default {
    namespaced: true,
    state: () => ({
        carList: []
    }),
    mutations: {
        changeCarList(state, list) {
            debugger
            console.log(list);

            state.carList = list.map((item) => {
                return { ...item, flag: true }
            })
        },
        addNub(state, id) {
            state.carList[state.carList.findIndex((item, index) => item.id == id)].count++
        },
        subtractNub(state, id) {
            state.carList[state.carList.findIndex((item, index) => item.id == id)].count--
        },
        changeFlag(state, flag) {
            state.carList.map((item, index) => {
                return item.flag = flag
            })
        }
    },
    getters: {
        allFlag(state) {
            return state.carList.every((item) => item.flag == true)
        },
        sumPrice(state) {
            return state.carList.reduce(
                (prev, cur) => prev + (cur.flag ? cur.count * cur.shopdata.price : 0), 0);
        }
    },
    actions: {
        async getCarData({ commit, rootState }, payload) {
            // let data = await getCarList({ user_id: rootState.login.user.uid });
            let data = await getCarList({ user_id: 5 });
            debugger
            commit("changeCarList", data.data)
            debugger
        }
    }
}